package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityEditBatteryCanHandlerBinding
import com.c.ctgproject.entity.guang.BatteryInpuOtherEntity
import com.c.ctgproject.entity.guang.PowerBatteryEntityV2
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.c.ctgproject.wiget.PhotoListShowRecyclerView
import com.c.ctgproject.wiget.dialog.MyBottomDialog
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import com.nanchen.compresshelper.CompressHelper
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
import java.io.File

class EditBatteryCanHandlerActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityEditBatteryCanHandlerBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mData by lazy { intent.getSerializableExtra(MyConstants.DATA) as PowerBatteryEntityV2.PowerBatteryBean }

    //图片处理
    private val imageShowList = ArrayList<String>()

    //图片处理
    private val inStockPictures = ArrayList<String>()
    private val imageResultCode = 100
    private val inStockPictureCode = 200
    private var status: Int? = null
    override fun event() {

        if (mData.orderId == null) {
            mBinding.bulkInOrderNo.isEnabled = true
        }
        status = mData.status
//        mBinding.mPhotoNGV.init(this, imageShowList, 3, imageResultCode)
//        mBinding.mPartsNGV.init(this, inStockPictures, 3, inStockPictureCode)
        //预处理图片
        mData.pretreatmentPictures?.let { images ->
//            val resultList: MutableList<NineGridBean> = ArrayList()
//            images.forEach { bean ->
//                val nineGirdData = NineGridBean(bean)
//                imageShowList.add(bean)
//                resultList.add(nineGirdData)
//            }
            mBinding.mPhotoNGV.addPhotos(images)
        }
        //散件
        mData.inStockPictures?.let { images ->
//            val resultList: MutableList<NineGridBean> = ArrayList()
//            images.forEach { bean ->
//                val nineGirdData = NineGridBean(bean)
//                inStockPictures.add(bean)
//                resultList.add(nineGirdData)
//            }
            mBinding.mPartsNGV.addPhotos(images)

        }
        mBinding.batteryCode.textSet(mData.batteryCode.handlerNull())
//        mBinding.count.textSet(mData.count.handlerNull())
        mBinding.capacityAh.textSet(mData.capacityAh.handlerNull())
        mBinding.voltage.textSet(mData.voltage.handlerNull())
        mBinding.capacityKwh.textSet(mData.capacityKwh.handlerNull())
        mBinding.stockPosi.textSet(mData.stockPosi.handlerNull())
        mBinding.mRemarkET.textSet(mData.remark.handlerNull())
        mBinding.moduleCount.textSet(mData.moduleCount.handlerNull())
        if (mData.bulkInOrderNo.handlerNull().isNotEmpty()) {
            mBinding.bulkInOrderNo.textSet(mData.bulkInOrderNo.handlerNull())
        }
        mBinding.weight.textSet(mData.weight.handlerNull())
        mBinding.mPhotoNGV.setPhotoListShowCallBack(object : PhotoListShowRecyclerView.PhotoListShowCallBack {
            override fun selectedPhoto(urls: List<String>): Boolean {
                upDataFile(listToFiles(urls)) {
                    val list = ArrayList<String>()
                    it?.forEach {
                        list.add(it.fileUrl.handlerNull())
                    }
                    mBinding.mPhotoNGV.addPhotos(list)
                }
                return false
            }

            override fun deletePhoto(url: String): Boolean {
                return false
            }

        })
        mBinding.mPartsNGV.setPhotoListShowCallBack(object : PhotoListShowRecyclerView.PhotoListShowCallBack {
            override fun selectedPhoto(urls: List<String>): Boolean {
                upDataFile(listToFiles(urls)) {
                    val list = ArrayList<String>()
                    it?.forEach {
                        list.add(it.fileUrl.handlerNull())
                    }
                    mBinding.mPartsNGV.addPhotos(list)
                }
                return false
            }

            override fun deletePhoto(url: String): Boolean {
                return false
            }

        })
//        mBinding.characterSelectET.text = when (mData.returnStatus) {
//            1 -> "已退库"
//            else -> {
//                mBinding.mCharacterSelectLL.isEnabled=false
//                "已入库"
//            }
//        }
        mBinding.characterSelectET.text = when (mData.status) {
            0 -> "待入库"
            1 -> "已入库"
            3 -> "已出库"
            else -> ""
        }
    }

    override fun click() {
        //入库状态
//        mBinding.mCharacterSelectLL.setOnClickListener {
//            // 0-待入库 1-已入库 3已出库
//            //已退库可以改成已入库，已入库不能改成已退库(要走退库申请，审批流程)
//            val list = mutableListOf("已退库","已入库")
//            val dialog = MyBottomDialog.Builder(this).setOnOptionSelectedListener { position ->
//                mBinding.characterSelectET.text = list[position]
//                if(position==0){
//                    status=mData.status
//                }else if(position==1){
//                    status=1
//                }
//            }.setOptions(list).create()
//            dialog.show()
//        }
        mBinding.voltage.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
        mBinding.capacityAh.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.voltage.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
        mBinding.capacityKwh.handlerDecimal3()


        mBinding.mConfirmBTN.setOnClickListener {

            submitToHttp()
            //所有本地文件的图片
//            val filePathList = ArrayList<String>()
//            val filePathList2 = ArrayList<String>()
//            imageShowList.forEach {
//                if (!it.startsWith("http")) {
//                    filePathList.add(it)
//                }
//            }
//            inStockPictures.forEach {
//                if (!it.startsWith("http")) {
//                    filePathList2.add(it)
//                }
//            }
//            val fileList = listToFiles(filePathList)
//            val fileList2=listToFiles(filePathList2)
//
//            upDataFile(fileList){
//                    val dataList = ArrayList<String>()
//                    it?.forEach { b ->
//                        dataList.add(b.fileUrl.handlerNull())
//                    }
//                    MyLog.d("有新的文件")
//                    //旧的也给他
//                    imageShowList.forEach { bean ->
//                        if (bean.startsWith("http")) {
//                            dataList.add(bean)
//                        }
//                    }
//                upDataFile(fileList2){
//                    val dataList2 = ArrayList<String>()
//                    it?.forEach { b ->
//                        dataList2.add(b.fileUrl.handlerNull())
//                    }
//                    MyLog.d("有新的文件")
//                    //旧的也给他
//                    inStockPictures.forEach { bean ->
//                        if (bean.startsWith("http")) {
//                            dataList2.add(bean)
//                        }
//                    }
//                    submitToHttp(dataList,dataList2)
//                }
//
//            }
//            if (filePathList.isNotEmpty()) {
//                val fileList = listToFiles(filePathList)
//
//                upDataFile(fileList) {
//                    val dataList = ArrayList<String>()
//                    it?.forEach { b ->
//                        dataList.add(b.fileUrl.handlerNull())
//                    }
//                    MyLog.d("有新的文件")
//                    //旧的也给他
//                    imageShowList.forEach { bean ->
//                        if (bean.startsWith("http")) {
//                            dataList.add(bean)
//                        }
//                    }
//
//                    submitToHttp(dataList)
//                }
//            } else {
//                //没有新文件 但是有旧的url
//                if (imageShowList.isNotEmpty()) {
//                    MyLog.d("没有新文件给他旧url")
//                    submitToHttp(imageShowList)
//                } else {
//                    MyLog.d("啥都没有")
//                    submitToHttp(arrayListOf())
//                }
//            }
        }
    }

    private fun listToFiles(filePathList: List<String>): ArrayList<File> {
        val fileList = ArrayList<File>()
        var index = 0
        filePathList.forEach {
            index++
            val file = File(it)
            //图片压缩
//                            val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                    val newFile = ImageUtil.imageWaterMark(this, file, index)
            val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
            fileList.add(newFile)
        }
        return fileList
    }

    private fun submitToHttp() {
        val map = HashMap<String, Any?>()
        val bulkInOrderNo=mBinding.bulkInOrderNo.text.handlerNull()
        if(bulkInOrderNo.isNullOrEmpty()){
            showToast("请先输入入库编码")
            return
        }
        map["pbId"] = mData.pbId
        map["status"] = status
        //当改成已入库时置空其他时候原样传
        if (status != 1) {
            map["returnStatus"] = mData.returnStatus
        }
        map["batteryCode"] = mBinding.batteryCode.text.handlerNull()
//            map["count"] = mBinding.count.text.handlerNull()
        map["capacityAh"] = mBinding.capacityAh.text.handlerNull()
        map["bulkInOrderNo"] = bulkInOrderNo
        map["voltage"] = mBinding.voltage.text.handlerNull()
        map["capacityKwh"] = mBinding.capacityKwh.text.handlerNull()
        map["remark"] = mBinding.mRemarkET.text.handlerNull()
        map["stockPosi"] = mBinding.stockPosi.text.handlerNull()
        map["pretreatmentPictures"] = mBinding.mPhotoNGV.getPhotos()
        map["weight"] = mBinding.weight.text.handlerNull()
        map["inStockPictures"] = mBinding.mPartsNGV.getPhotos()
        map["moduleCount"]=mBinding.moduleCount.text.handlerNull()
        ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryInputOther(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调:$it")
            showToast("保存成功")
            finish()
        })
    }

//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        data?.let {
//            when (requestCode) {
//                //图片回调
//                imageResultCode -> {
//                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
//                    val resultList: MutableList<NineGridBean> = ArrayList()
//                    list?.forEach { imageBean ->
//                        val nineGirdData = NineGridBean(imageBean.imagePath)
//                        imageShowList.add(imageBean.imagePath)
//                        resultList.add(nineGirdData)
//                    }
//                    mBinding.mPhotoNGV.addDataList(resultList)
//                }
//                inStockPictureCode->{
//                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
//                    val resultList: MutableList<NineGridBean> = ArrayList()
//                    list?.forEach { imageBean ->
//                        val nineGirdData = NineGridBean(imageBean.imagePath)
//                        inStockPictures.add(imageBean.imagePath)
//                        resultList.add(nineGirdData)
//                    }
//                    mBinding.mPartsNGV.addDataList(resultList)
//                }
//                else -> {
//
//                }
//            }
//        }
//    }
}